<template>
  <!-- 授权用户 -->
  <a-modal title="选择用户" v-model:visible="visible" width="840px" title-align="start">
    <template #title>
      选择用户
    </template>
    <a-form :model="queryParams" ref="queryRef" layout="inline">
      <a-form-item label="用户名称" field="userName">
        <a-input
            size="small"
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </a-form-item>
      <a-form-item label="手机号码" field="phonenumber">
        <a-input
            size="small"
            v-model="queryParams.phonenumber"
            placeholder="请输入手机号码"
            clearable
            @keyup.enter="handleQuery"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" size="small" class="button-margin-right" @click="handleQuery">
          <icon-Search/>
          搜索
        </a-button>
        <a-button @click="resetQuery" size="small">
          <icon-loop/>
          重置
        </a-button>
      </a-form-item>
    </a-form>
    <a-table ref="refTable" row-key="userId" :data="userList" :row-selection="userRowSelection"
             v-model:selectedKeys="selectedUserIds" :pagination="false" scroll="{y: 260}">
      <template #columns>
        <a-table-column title="用户名称" data-index="userName" :show-overflow-tooltip="true"/>
        <a-table-column title="用户昵称" data-index="nickName" :show-overflow-tooltip="true"/>
        <a-table-column title="邮箱" data-index="email" :show-overflow-tooltip="true"/>
        <a-table-column title="手机" data-index="phonenumber" :show-overflow-tooltip="true"/>
        <a-table-column title="状态" align="center" data-index="status">
          <template #cell="{ record }">
            <dict-tag :options="sys_normal_disable" :value="record.status"/>
          </template>
        </a-table-column>
        <a-table-column title="创建时间" align="center" data-index="createTime" width="180">
          <template #cell="{ record }">
            <span>{{ parseTime(record.createTime) }}</span>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <template #footer>
      <div class="dialog-footer">
        <a-button type="primary" size="small" class="button-margin-right" @click="handleSelectUser">确 定</a-button>
        <a-button size="small" @click="visible = false">取 消</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script setup name="SelectUser">
import {authUserSelectAll, unallocatedUserList} from "@/api/system/role";
import {getCurrentInstance, reactive, ref} from "vue";

const props = defineProps({
  roleId: {
    type: [Number, String]
  },
});

const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");

const userList = ref([]);
const visible = ref(false);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  roleId: undefined,
  userName: undefined,
  phonenumber: undefined
});

// 显示弹框
function show() {
  queryParams.roleId = props.roleId;
  getList();
  visible.value = true;
}

/**选择行 */
function clickRow(row) {
  proxy.$refs["refTable"].toggleRowSelection(row);
}

// 多选框选中数据
const selectedUserIds = ref([]);
const userRowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
  onlyCurrent: false,
});

// 查询表数据
function getList() {
  unallocatedUserList(queryParams).then(res => {
    userList.value = res.rows;
    total.value = res.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

const emit = defineEmits(["ok"]);

/** 选择授权用户操作 */
function handleSelectUser() {
  const roleId = queryParams.roleId;
  const uIds = selectedUserIds.value.join(",");
  if (uIds == "") {
    proxy.$modal.msgError("请选择要分配的用户");
    return;
  }
  authUserSelectAll({roleId: roleId, userIds: uIds}).then(res => {
    proxy.$message.success(res.msg);
    if (res.code === 200) {
      visible.value = false;
      emit("ok");
    }
  });
}

defineExpose({
  show,
});
</script>
